package com.yl.androidutilscenter.http

import com.yl.androidutilscenter.utils.ext.isGsonFormat
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody

/**
 * @author YL
 * @description:响应数据拦截器
 * @date :2024/4/25
 * @version 1.0.0
 */
abstract class NetResultInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        var response = chain.proceed(chain.request())
        response.body()?.run {
            contentType()?.let { mediaType ->
                val strResponse = string()
                if (strResponse.isGsonFormat()) {
                    parseData(strResponse)
                }
                val responseBody = ResponseBody.create(mediaType, strResponse)
                response = response.newBuilder().body(responseBody).build()
            }
        }
        return response
    }


    /**
     * @param gsonResponse  response对应的string数据，已做个是否是gson格式的判断
     */
    abstract fun parseData(gsonResponse: String)
}